﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Takkou.Core
{
    public static class Game
    {
        public static World World { get; private set; }
        public static Player Player { get; private set; }
        public static UserControl Screen { get; private set; }

        public static long ElapsedTime { get; private set; }
        public static bool Running { get; set; }

        private static long lastTime = 0;

        public static void Update(long ticks)
        {
            if (Running)
            {
                ElapsedTime = ticks - lastTime;
                foreach (WorldElement elm in World.getElements())
                {
                    elm.update();
                    UpdateElementSprite(elm);
                }
            }
            lastTime = ticks;
        }

        private static void UpdateElementSprite(WorldElement elm)
        {
            elm.Sprite.SetValue(Canvas.LeftProperty, elm.Pos.X - elm.Offset.X);
            elm.Sprite.SetValue(Canvas.TopProperty, elm.Pos.Y - elm.Offset.Y);
            elm.Sprite.RenderTransform = new RotateTransform
            {
                Angle = elm.Pos.Dir,
                CenterX = elm.Offset.X,
                CenterY = elm.Offset.Y
            };
        }

        public static void loadWorld(string[][] state)
        {
            for (int i = 0; i < state.Length; ++i)
            {
                int dir;
                int x = int.Parse(state[i][2]);
                int y = int.Parse(state[i][3]);
                switch (state[i][0])
                {
                    case "WORLD":
                        World = new World(state[i][1], x, y);
                        break;
                    case "BASE":
                        Base b = new Base(state[i][1], x, y);
                        World.addWorldElement(b);
                        break;
                    case "ROBOT":
                        dir = int.Parse(state[i][4]);
                        Robot rob = new RepeatRobot(state[i][1], x, y, dir);
                        World.addWorldElement(rob);
                        break;
                    case "MINER":
                        dir = int.Parse(state[i][4]);
                        Miner m = new Miner(state[i][1], x, y, dir);
                        World.addWorldElement(m);
                        break;
                    case "RESOURCE":
                        Resource.MaterialType type = (Resource.MaterialType)Enum.Parse(typeof(Resource.MaterialType), state[i][4], true);
                        int capacity = int.Parse(state[i][5]);
                        Resource rsc = new Resource(state[i][1], type, x, y, capacity);
                        World.addWorldElement(rsc);
                        break;
                }
            }
        }


        public static void setupScreen(UserControl gameScreen)
        {
            Screen = gameScreen;
            Running = false;
            foreach (WorldElement elm in World.getElements())
            {
                UpdateElementSprite(elm);
                Canvas worldArea = (Canvas) gameScreen.FindName("WorldArea");
                worldArea.Children.Add(elm.Sprite);
            }
        }
    }



}
